# encoding=utf-8
from LBT.BaseParent.BaseLbt import lbtlogin
from LBT.Resource.StudentAttendance.ClassAttendanceAtt import ClassAttendanceAtt
from LBT.GeneralOperation.StudentOperation import StudentOperation


class ClassAttendance(lbtlogin, ClassAttendanceAtt):
    '''班级考勤统计模块
    1、跟进数据库查询首个有数据的班级和时间，然后通过条件搜索班级、时间、查询。校验
    查询结果和数据库，数据库和导出结果。
    2、默认条件，进入后查找第一条数据，查看未出勤详情和数据库比对。
    '''

    @lbtlogin.loginFun
    def test_checkLastAtt(self):
        """
        跟进数据库查询首个有数据的班级和时间，然后通过条件搜索班级、时间、查询。校验查询结果和数据库，数据库和导出结果。
        :return:
        """
        self.docstr = '跟进数据库查询首个有数据的班级和时间，然后通过条件搜索班级、时间、查询。校验查询结果和数据库，数据库和导出结果。'
        dr = self.driver
        self.moduleOp.openMoudleAndIn(self.studentatten_navigation_class, self.studentatten_class_url,
                                      self.student_topnavigation_class)
        # 查询数据库查到本学校所有出勤统计
        sql = "select tt,count(nn),nn from( select distinct(att.studentid),convert(varchar(10),cardtime,121) tt,edu.eduunitname as nn From attendance att inner join student st on st.studentid=att.studentid inner join eduunit edu on edu.eduunitid = st.eduunitid where att.schoolid = (select schoolid from school where schoolname = '" + self.lbt_schoolname_text + "') and edu.state=1 ) bb group by tt,nn order by tt desc"
        infolist = self.selectlbtmutilcolumn(sql)
        classname = str(infolist[0][2])
        num = str(infolist[0][1])
        time = str(infolist[0][0])
        # 根据首条结果，查询页面和数据库进行核对
        stuopr = StudentOperation(dr)
        stuopr.selectWithInput(dr, 0, 0, classname)
        selecttime = time + " 至 " + time
        self.clearAndSendKeyForText(dr.find_element_by_id(self.classatt_selecttime_id), selecttime)
        dr.find_element_by_css_selector(self.classatt_selectbtn_css).click()
        results = dr.find_elements_by_xpath(self.classatt_results_xpath)
        self.assertEqual(results.__len__(), 1)
        resultinfo = {}
        resultinfo["innum"] = int(results[0].text.split(" ")[3])
        resultinfo["datestr"] = str(results[0].text).split(" ")[0]
        resultinfo["classname"] = str(results[0].text.split(" ")[2])
        self.assertEqual(int(num), resultinfo["innum"])
        self.assertIn(time, resultinfo["datestr"])
        self.assertIn(classname, resultinfo["classname"])
        # 导出刚才查询的那一条，然后和数据库核对
        self.deleteAllFiles(self.defaultdownpath)
        dr.find_element_by_css_selector(self.classatt_exportbtn_css).click()
        downlist = self.readExcle(self.defaultdownpath)
        self.assertEqual(int(num), int(downlist[1][2]))
        self.assertIn(str(time), str(downlist[1][0]))
        self.assertIn(str(classname), str(downlist[1][1]))

    @lbtlogin.loginFun
    def test_checkNoAtt(self):
        """
        默认条件，进入后查找第一条数据，查看未出勤详情和数据库比对。
        :return:
        """
        self.docstr = '默认条件，进入后查找第一条数据，查看未出勤详情和数据库比对。'
        dr = self.driver
        self.moduleOp.openMoudleAndIn(self.studentatten_navigation_class, self.studentatten_class_url,
                                      self.student_topnavigation_class)
        # 查询首条数据
        dr.find_element_by_css_selector(self.classatt_selectbtn_css).click()
        results = dr.find_elements_by_xpath(self.classatt_results_xpath)
        resultinfo = {}
        resultinfo["innum"] = int(results[0].text.split(" ")[3])
        resultinfo["datestr"] = str(results[0].text).split(" ")[0]
        resultinfo["classname"] = str(results[0].text.split(" ")[2]).split("（")[0]
        dr.find_elements_by_css_selector(self.classatt_noatt_css)[0].click()
        dr.switch_to.frame(self.classatt_detailframe_id)
        studentnamelisttext = dr.find_element_by_css_selector(self.classatt_noattdetail_css).text
        studentnamelist = str(studentnamelisttext).split("\n")
        # 查询数据库查到对应班级对应天数的出勤统计
        sql = "select name from users u inner join student st on st.studentid=u.usersid where u.schoolid = (select schoolid from school where schoolname =  '" + self.lbt_schoolname_text + "')  and u.usersid not in ( select distinct(att.studentid) From attendance att inner join student st on st.studentid=att.studentid inner join eduunit edu on edu.eduunitid = st.eduunitid where att.schoolid = (select schoolid from school where schoolname =  '" + self.lbt_schoolname_text + "') and edu.state=1 and edu.eduunitname='" + \
              resultinfo["classname"] + "' and convert(varchar(10),cardtime,121)='" + resultinfo[
                  "datestr"] + "') and st.eduunitid = (select eduunitid from eduunit where eduunitname='" + resultinfo[
                  "classname"] + "'  and u.state=1 ) "
        infolist = self.selectlbtmutilcolumn(sql)
        for a in infolist:
            self.assertIn(str(a[0]),studentnamelist)